/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller;

import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.spec.RSAPublicKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.ds.MockDS;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.model.utils.SystemProperties;
import org.cx1.service.api.IAuthService;
import org.cx1.service.api.IUserService;
import org.cx1.service.impl.DAuthService;
import org.cx1.service.impl.DUserService;
import org.cx1.service.impl.MAuthService;
import org.cx1.service.impl.MUserService;
import org.cx1.service.utils.UAgentInfo;
import org.cx1.web.utils.JCryptionUtil;
import org.cx1.web.utils.SessionUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author ECH
 */
public class LoginController extends BaseController {
    
    //private IAuthService loginService = new MAuthService();
    private IAuthService loginService = new DAuthService();
    private IUserService userService = new DUserService();
    
    public LoginController() {
        
    }
    
    protected ModelAndView handleRequestInternal( HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();
        
        int action = StringUtils.isEmpty( request.getParameter("a") ) ? 0 : Integer.parseInt(request.getParameter("a"));
        
        switch (action) {
            default: {
                authenticate(mav, map, request);
                break;
            }
        }        
        
        map.put("host", SystemProperties.SYSTEM_HOST);
        map.put("port", SystemProperties.SYSTEM_PORT);
        map.put("props", this.getSystemProperties());
        mav.addObject("model", map);
        return mav;
    }
    
    private void authenticate(ModelAndView mav, Map map, HttpServletRequest request) {
        String loginFailed = StringUtils.isEmpty(request.getParameter("loginFailed")) ? "0" : "1";        
        if (!StringUtils.isEmpty(request.getParameter("s"))) {
            String uname = request.getParameter("uname");
            String pass = request.getParameter("pass");
                        
            ServiceResponse<String> response = loginService.authenticate2(uname, pass);
            
            if (response.getResponse() != null && !StringUtils.isEmpty(response.getResponse())) {
                mav.setView( new RedirectView("account.htm") );
                request.getSession().invalidate();                
                SessionUtils.setUserID(response.getResponse(), request.getSession(true));
                userService.refreshLastLogin(response.getResponse());
            }            
            else {
                loginFailed = "1";
                mav.setView( new RedirectView("login.htm?loginFailed=1") );
            }
        }
        else {               
            if (this.isThisRequestCommingFromAMobileDevice(request)) {
                mav.setViewName("login_mobile");
            }
            else {
                mav.setViewName("login");                
            }
        }
        map.put("loginFailed", loginFailed);
    }
    
}
